package com.gpzx.zx.module.home.frame.view

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.gpzx.zx.R
import com.gpzx.zx.module.home.frame.model.OrgInfo
import com.gpzx.zx.module.home.ui.activity.OrgInfoActivity
import com.gpzx.zx.module.home.ui.activity.OrgTypeListActivity
import com.gpzx.zx.module.home.ui.adapter.OrgAdapter
import com.qmuiteam.qmui.widget.QMUITopBar
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.wkq.base.frame.mosby.delegate.MvpView
import com.wkq.lib_base.adapter.KtDataBindingAdapter
import com.wu.common.utils.StatusBarUtil

/**
 *
 * 作者:吴奎庆
 *
 * 时间:2020/7/4
 *
 * 用途:
 */


class OrgTypeListView : MvpView {

    var mActivity: OrgTypeListActivity
    var mAdapter: OrgAdapter? = null

    constructor(mActivity: OrgTypeListActivity) {
        this.mActivity = mActivity
    }

    private fun initToolBar(title: String) {
        StatusBarUtil.setStatusBarDarkMode(mActivity)
        StatusBarUtil.setColor(mActivity, mActivity!!.getResources().getColor(R.color.color_2b2b2b), 0)
        var toolbar = mActivity!!.binding.includeToolbar as QMUITopBar
        toolbar.setTitle(title)
        toolbar.setBackgroundColor(mActivity!!.resources.getColor(R.color.color_2b2b2b))
        toolbar.addLeftImageButton(R.drawable.ic_arrow_back_white_24dp, R.id.qmui_topbar_item_left_back).setOnClickListener { mActivity!!.finish() }
    }


    fun initView(title: String) {
        initToolBar(title)
        mActivity.binding.rvContent.layoutManager = LinearLayoutManager(mActivity)
        mAdapter = OrgAdapter(mActivity as Context, R.layout.item_org_list)
        mActivity.binding.rvContent.adapter = mAdapter

        mActivity.binding.sfLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                mActivity.presenter.initData(mActivity, mActivity.title)
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                mActivity.presenter.initData(mActivity, mActivity.title)
            }

        })
        mAdapter!!.setOnViewClickListener(object : KtDataBindingAdapter.OnAdapterViewClickListener<OrgInfo> {
            override fun onViewClick(v: View?, program: OrgInfo?) {
                OrgInfoActivity().newInstance(mActivity as Context,program?.id+"")
            }
        })
    }

    fun showList(list: MutableList<OrgInfo>) {
        var orgTypes: MutableList<OrgInfo> = ArrayList()
      var tpes=  list.toMutableList()
        var name:String=""

        tpes.forEach {
            if ( it.o_name?.equals(name) == false){
                orgTypes.add(it)
                name= it.o_name!!
            }
        }
        mAdapter?.removeAllItems()
        mAdapter?.addItems(orgTypes)
        mActivity.binding.sfLayout.finishRefresh()
        mActivity.binding.sfLayout.finishLoadMore()

    }

}